package com.parachutes.aspinterfacecore.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.parachutes.aspinterfaceapi.domain.BusInterface;
import com.parachutes.aspinterfaceapi.domain.BusInterfaceInvokeRecord;
import com.parachutes.aspinterfaceapi.domain.Result;
import com.parachutes.aspinterfaceapi.domain.dto.BusInterfaceDTO;
import com.parachutes.aspinterfaceapi.service.BusInterfaceService;
import com.parachutes.aspsystemapi.domian.UserInterfaceSubscribe;
import com.parachutes.aspsystemapi.feign.UserInterfaceSubscribeFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author machi
 * @date 2025/01/29
 */
@RestController
@RequestMapping("/info")
public class BusInterfaceController {

    @Resource
    private BusInterfaceService busInterfaceService;

    @Resource
    UserInterfaceSubscribeFeign userInterfaceSubscribeFeign;
    /**
     * 列出所有接口信息
     *
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping("list")
    public Result<?> list( BusInterfaceDTO busInterfaceDTO) {
        PageMethod.startPage(busInterfaceDTO.getPage(), busInterfaceDTO.getPageSize());
        List<BusInterface> list = busInterfaceService.getList(busInterfaceDTO);
        PageInfo<BusInterface> pageInfo = new PageInfo<>(list);
        return Result.of(true, "获取成功", pageInfo);
    }

    /**
     * 列出所有接口信息
     *
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping("list-by-ids")
    public Result<?> listByIds(String ids,Integer page, Integer size) {
        PageMethod.startPage(page, size);
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        List<BusInterface> list = busInterfaceService.listByIds(idList);
        PageInfo<BusInterface> pageInfo = new PageInfo<>(list);
        return Result.of(true, "获取成功", pageInfo);
    }

    //新增接口信息
    @PostMapping("insert")
    public Result<?> add(@RequestBody BusInterface busInterface) {
        String string = UUID.randomUUID().toString();
        busInterface.setInterfaceKey(string);
        busInterfaceService.save(busInterface);
        return Result.of(true, "新增成功");
    }

    //修改接口信息
    @PutMapping("update")
    public Result<?> update(@RequestBody BusInterface busInterface) {
        busInterfaceService.updateById(busInterface);
        return Result.of(true, "修改成功");
    }

    //删除接口信息
    @DeleteMapping("{id}")
    public Result<?> delete(@PathVariable("id")Integer id) {
        busInterfaceService.removeById(id);
        return Result.of(true, "禁用成功");
    }

    /**
     * 列出所有待审核接口信息
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("un-review")
    public Result<?> getUnReview(Integer page, Integer pageSize) {
        PageMethod.startPage(page, pageSize);
        LambdaQueryWrapper<BusInterface> queryWrapper = Wrappers.lambdaQuery(BusInterface.class).eq(BusInterface::getStatus, 0);
        List<BusInterface> list = busInterfaceService.list(queryWrapper);
        PageInfo<BusInterface> pageInfo = new PageInfo<>(list);
        return Result.of(true, "获取成功", pageInfo);
    }

    @GetMapping("generate-key")
    public Result<String> generateKey(Integer interfaceId,Integer userId) {
        String secret = UUID.randomUUID().toString();
        UserInterfaceSubscribe userInterfaceSubscribe = new UserInterfaceSubscribe();
        userInterfaceSubscribe.setInterfaceId(interfaceId);
        userInterfaceSubscribe.setUserId(userId);
        userInterfaceSubscribe.setInterfaceSecret(secret);
        userInterfaceSubscribeFeign.updateSubscribed(userInterfaceSubscribe);
         return Result.of(true, "生成成功", secret);
    }
}
